let checkedUserList = []
let checkedTasklist = []

window.addEventListener("load", () => {
    document.getElementById("managerLoginNumberAndName").innerText = `Manager ID:${Cookies.get('id')} Name:${Cookies.get('username')}`
    let date = new Date();
    // `${date.getFullYear()} ${date.getMonth()+1} ${date.getDate()+1} 00:00:00`;
    $.post("/manager/selectAllTasks", null, (res) => {
        if (res.status == "3001") {
            alert(res.message)
            document.location = "/public/html/home.html"
        } else if (res.data) {


            let selectTaskTable = document.querySelector("#selectTaskTable>tbody");

            for (let resArrayElement of res.data) {
                let tr = document.createElement("tr")
                let rowID
                for (let objKeyAndValue in resArrayElement) {
                    if (objKeyAndValue == "id") {
                        rowID = resArrayElement[`${objKeyAndValue}`]
                    }
                    let td = document.createElement("td")
                    if (objKeyAndValue == "due_date") {
                        let timeBigInt = resArrayElement[`${objKeyAndValue}`] * 1000
                        let date = new Date(timeBigInt)

                        td.innerText = `${date.toLocaleString()}`
                    } else {
                        td.innerText = resArrayElement[`${objKeyAndValue}`]
                    }

                    tr.appendChild(td)

                }
                let tdOpration = document.createElement("td")
                let input = document.createElement("input")
                input.setAttribute("type", "checkbox")
                input.setAttribute("data-id", rowID)

                input.addEventListener("click", (event) => {
                    let checkKey = event.target.getAttribute("data-id")
                    if (input.checked) {
                        checkedTasklist.push(checkKey)
                    } else {
                        checkedTasklist.pop(checkKey)
                    }
                })
                tdOpration.appendChild(input)
                tr.appendChild(tdOpration)

                selectTaskTable.appendChild(tr)
            }
        } else {
            let selectUserTable = document.querySelector("#selectUserTable>tbody");
            let tr = document.createElement("tr")
            let td = document.createElement("td")
            td.innerText = "No Today Tasks"
            tr.appendChild(td)
            selectUserTable.appendChild(tr)
        }


    }, "json")
    $.post("/manager/selectAllUsers", null, (res) => {
        if (res.data) {
            let selectUserTable = document.querySelector("#selectUserTable>tbody");

            for (let resArrayElement of res.data) {
                let tr = document.createElement("tr")
                let rowID
                for (let objKeyAndValue in resArrayElement) {
                    if (objKeyAndValue == "id") {
                        rowID = resArrayElement[`${objKeyAndValue}`]
                    }
                    let td = document.createElement("td")
                    if (objKeyAndValue == "due_date") {
                        let timeBigInt = resArrayElement[`${objKeyAndValue}`] * 1000
                        let date = new Date(timeBigInt)

                        td.innerText = `${date.toLocaleString()}`
                    } else {
                        td.innerText = resArrayElement[`${objKeyAndValue}`]
                    }

                    tr.appendChild(td)

                }
                let tdOpration = document.createElement("td")
                let input = document.createElement("input")
                input.setAttribute("type", "checkbox")
                input.setAttribute("data-id", rowID)

                input.addEventListener("click", (event) => {
                    let checkKey = event.target.getAttribute("data-id")
                    if (input.checked) {
                        checkedUserList.push(checkKey)
                    } else {
                        checkedUserList.pop(checkKey)
                    }
                })
                tdOpration.appendChild(input)
                tr.appendChild(tdOpration)

                selectUserTable.appendChild(tr)

            }
        } else {
            let selectUserTable = document.querySelector("#selectUserTable>tbody");
            let tr = document.createElement("tr")
            let td = document.createElement("td")
            td.innerText = "No Today Tasks"
            tr.appendChild(td)
            selectUserTable.appendChild(tr)

        }

    }, "json")

})

document.getElementById("managerAssignButton").addEventListener("click", () => {


    for (let checkedTasklistElement of checkedTasklist) {
        for (let checkedUserListElement of checkedUserList) {
            $.post("/manager/assignTaskToUser", {
                "userid": checkedUserListElement,
                "taskid": checkedTasklistElement
            }, function (res) {
                alert(`${res.message}`)
            }, "json")
        }
    }


})